<?php

class TestWakTextFile extends UnitTestCase {

    function setUp() {
        $path=dirname(__FILE__);
        $this->_deleteIfExists("$path/text.txt");
    }

    function testText() {
        $file=dirname(__FILE__).'/text.txt';
        $line='one line';
        $content='Some content';
        $f=new WakTextFile($file);

        $this->assertTrue($f->setContent($content)
            ,"setContent() should set content at once");
        $this->assertTrue($f->exists()
            ,"...and shall create file if didn't exist");
        $this->assertEqual($f->getContent(),$content
            ,"getContent() shall return whole content at once %s");
        $this->assertTrue($f->newline(' and a new line')
            ,"newLine() shall return true");
        $this->assertEqual($f->getContent(),$content.chr(13).chr(10).' and a new line'
            ,"... and add a new line at the end of file %s");

        $this->assertTrue(($f->setContent('shorter content')
        and $f->getContent()=='shorter content')
            ,"setContent should erase previous content (in case enwer content is shorter)");
    }

    function _deleteIfExists($file) {
        if (file_exists($file) and !unlink($file)) {
            $this->fail("cant' start tests, file '.$file.' should not exist before tests %s'");
            return false;
        }
        return true;
    }
}
